package com.ba.base.view

import android.content.ComponentCallbacks2
import android.graphics.Color
import android.os.Bundle
import android.os.PersistableBundle
import androidx.core.content.ContextCompat
import com.ba.base.R
import com.ba.base.api.ApiAuth
import com.ba.base.utils.LogUtils
import com.ba.base.utils.ThemeHelper
import com.screx.mcloud.lib.view.AbsThemeActivity
import qsos.lib.base.utils.ActivityManager


abstract class AbsActivity : AbsThemeActivity() {
    val STATE_TOKEN = "token"
    override fun notifyTheme(data: ThemeData) {
        ThemeHelper.setColor(this, Color.parseColor("#F8F8F8"), 0, true)
        mToolbar?.setBackgroundColor(Color.WHITE)
        mTitle?.setTextColor(Color.BLACK)
        setBack(ContextCompat.getDrawable(mContext, R.drawable.icon_back_dark)!!)
    }

//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        //判断app状态
//        if (AppStatusManager.instance?.appStatus === AppStatus.STATUS_RECYCLE) {
//            //被回收，跳转到启动页面
////            val intent = Intent(this, SplashActivity::class.java)
////            startActivity(intent)
////            finish()
//            ARouter.getInstance().build("/app/splash").navigation()
//            return
//        }
//    }
    /**
     * 内存不够时
     * @param level
     */
    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        LogUtils.d("onTrimMemory", level.toString())
        /**
         * level==ComponentCallbacks2.TRIM_MEMORY_COMPLETE
         * 表示手机目前内存已经很低了，并且我们的程序处于LRU缓存列表的最边缘位置，
         * 系统会最优先考虑杀掉我们的应用程序，
         * 在这个时候应当尽可能地把一切可以释放的东西都进行释放
         */
        if (level == ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
//            ActivityManager.finishAll()
        }
    }

    override fun onLowMemory() {
        super.onLowMemory()
        LogUtils.d("onTrimMemory","onLowMemory")
        ActivityManager.finishAll()
    }
    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        // 保存token
        outState.putString(STATE_TOKEN, ApiAuth.bearer)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        // 从已保存的实例中恢复状态成员
        ApiAuth.bearer = savedInstanceState.getString(STATE_TOKEN).toString()
    }

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        // 从已保存的实例中恢复状态成员
        ApiAuth.bearer = savedInstanceState?.getString(STATE_TOKEN).toString()
    }
    override fun initView() {
        super.initView()
        ThemeHelper.setLightMode(this)
    }

    private fun restartApp() {
//        val intent = Intent(this, MainActivity::class.java)
//        intent.putExtra(START_LAUNCH_ACTION, STATUS_FORCE_KILLED)
//        startActivity(intent)
    }
}